package my.framework.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseUtil {
    //数据库驱动字符串
    private static String driver = ConfigManager.getProperty("driver");
    //连接URL字符串
    private static String url = ConfigManager.getProperty("url");
    //数据库用户名
    private static String user = ConfigManager.getProperty("user");
    //用户密码
    private static String password = ConfigManager.getProperty("password");

    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<>();  //(1)

    /**
     * 获取数据库连接对象
     *
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        //获取连接并捕获异常
        Connection connection = threadLocal.get();  //(2)
        if (connection == null || connection.isClosed()) { //(3)
            try {
                connection = DriverManager.getConnection(url, user, password);
                if (connection.getAutoCommit()) {
                    connection.setAutoCommit(false);
                }
                threadLocal.set(connection);  //(4)
            } catch (SQLException e) {
                e.printStackTrace();
                throw e;
            }
        }
        return connection;
    }

    /**
     * 关闭数据库连接
     */
    public static void closeConnection() {   //(5)
        Connection connection = (Connection) threadLocal.get();
        threadLocal.set(null);

        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭语句容器
     *
     * @param stmt Statement对象
     * @param rs   结果集
     */
    public static void closeStatement(Statement stmt, ResultSet rs) {
        try {
            if (rs != null && rs.isClosed()) {
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (stmt != null && stmt.isClosed()) {
                stmt.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
